<template>
  <div>
    <img src="" alt="">
    <van-form @submit="onSubmit">
      <van-field v-model="username" name="userName" label="用户名" placeholder="用户名"
        :rules="[{ required: true, message: '请填写用户名' }]" />
      <van-field v-model="password" type="password" name="password" label="密码" placeholder="密码"
        :rules="[{ required: true, message: '请填写密码' }]" />
      <div style="margin: 16px;">
        <van-button round block type="info" native-type="submit">登录</van-button>
      </div>
    </van-form>

  </div>
  
</template>
<script>
import request from "../request/index"
import qs from 'qs'
export default {
  name: '',
  data() {
    return {
      username: '',
      password: '',
    };
  },
  methods: {
    onSubmit(values) {
      //发请求
      request({
        url: "/login",
        method: "post",
        data: values
      }).then(res => {
        console.log(res);
        //弹框提示
        this.$dialog.alert({
          message: "登录成功",
        }).then(() => {
          if (res.data.code == 0) {
            //登录成功 页面跳转
            localStorage.setItem("token", res.data.msg);
            this.$store.state.token = res.data.msg;
            this.$store.state.user = res.data.data;
            localStorage.setItem("user03",qs.stringify(res.data.data))
            //跳转页面
            this.$router.replace("/home");
          }
        });
        
      }).catch(err => {
        console.log(err)
      })
    },
  },
  computed: {
  },
  mounted() {
    //当前高度等于窗口高度
    document.getElementById("appview").style.height = document.documentElement.clientHeight+"px";
    console.log(this.$store.state.token);
    var temp = this.$store.state.token;
    //token存在发送请求向后端验证token真实性
    if(temp != ""){
      request({
        url: "/vertoken",
        method: "post"
      }).then(res=>{
        console.log(res);
        if(res.data.code == 0){
          console.log()
          this.$store.state.user = res.data.data;
          localStorage.setItem("user03",qs.stringify(res.data.data))
          // debugger
          this.$router.replace("/home");
        }
      }).catch(err=>{
        console.log(err)
      })
    }
  },
}
</script>